<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button id="btn_open">打开B页面</button>
  <button id="btn_send">发送数据</button>
  <button id="btn_iframe">iframe</button>
  <iframe src="./B.html" frameborder="0"></iframe>
</body>
<script>
  window.onload = function() {
    var btn_open = document.querySelector('#btn_open');
    var btn_send = document.querySelector('#btn_send');
    var btn_iframe = document.querySelector('#btn_iframe');
    var win;
    
    // 打开按钮点击事件
    btn_open.onclick = function() {
      win = window.open('./B.html', 'popUp');
    }
    // 发送按钮点击事件
    btn_send.onclick = function() {
      win.postMessage('hello B.htlm')
    }
    // iframe按钮点击事件
    btn_iframe.onclick = function() {
      console.log('win已切换至iframe');
      win = document.querySelector('iframe').contentWindow
    }
  }
</script>
</html>